//
//  BlogCommentCell.swift
//  mmda
//
//  Created by 李陆涛 on 2018/8/12.
//  Copyright © 2018年 mmda. All rights reserved.
//

import UIKit
import YYText


class BlogCommentCell: UITableViewCell {

    var comment:Comment? {
        
        didSet {
        
            reloadData()
        }
    }
    
    var pressedUser:((User?)->Void)?
    var longPressComment:(()->Void)?
    
    
    
    @IBOutlet weak var commentLabelH: NSLayoutConstraint!
    @IBOutlet weak var commentLabel: YYLabel!
    
    override func awakeFromNib() {
        super.awakeFromNib()
        
        commentLabel.numberOfLines = 0
        commentLabel.preferredMaxLayoutWidth = CGFloat(kScreenWidth - 48)
        
        commentLabel.textColor = UIColor.black
        commentLabel.font = kFont14
        
        self.addGestureRecognizer(UILongPressGestureRecognizer(target: self, action: #selector(longpressAction)))
    }
    
    
    @objc private func longpressAction() {
        
        self.longPressComment?()
    }
    
    func reloadData() {

        
        guard let comment = comment, let commentUser = comment.comment_user_info,let commentUserName = commentUser.nickname else { return }
        
        let commentStr = NSMutableAttributedString(string: commentUserName)
        
        
        commentStr.yy_setTextHighlight(commentStr.yy_rangeOfAll(), color: kBlue, backgroundColor: nil) {
            
            [weak self] _,_,_,_ in
            guard let `self` = self else { return }
            
            self.pressedUser?(commentUser)
        }

        
        if let toUer = comment.to_user_info, let toUserName = toUer.nickname  {

            let strreply = NSMutableAttributedString(string: "回复")
            strreply.yy_color = UIColor.black
            
            
            commentStr.append(strreply)
            
            let toStr = NSMutableAttributedString(string: toUserName)
            
            toStr.yy_setTextHighlight(toStr.yy_rangeOfAll(), color: kBlue, backgroundColor: nil) {
                [weak self] _,_,_,_ in
                guard let `self` = self else { return }
                
                self.pressedUser?(toUer)
            }
            
            commentStr.append(toStr)
        }
        
        let strremark = NSMutableAttributedString(string: "：")
        
        if let remark = comment.remark {
            
            strremark.yy_appendString(remark)
        }
        strremark.yy_color = UIColor.black
        
        commentStr.append(strremark)
//        commentStr.yy_lineSpacing = 8
        commentStr.yy_font = kFont14
        
        commentLabel.attributedText = commentStr

    
    let height = YYTextLayout(containerSize: CGSize(width: kScreenWidth - 48, height: Int.max), text: commentStr)?.textBoundingSize.height ?? 0
    
    self.commentLabelH.constant = height
        
    }
}
